<template>
  <div class="circleManage-container">
    <div class="circleManage-search">
      <span class="page-title">圈子列表</span>
      <el-input placeholder="请输入搜索内容" v-model="keyWords" class="input-with-select search-keyword">
        <el-select v-model="select" slot="prepend" placeholder="请选择">
          <el-option label="用户昵称" value="1"></el-option>
        </el-select>
      </el-input>
      <el-button type="primary" size="small" icon="el-icon-search" @click="searchCircle">搜索</el-button>
      <el-button type="info" size="small" class="search-keyword-reset" plain @click="resetSearch">重置</el-button>
      <!--<el-button type="primary" size="small" icon="el-icon-edit" @click="toggleSelection(circleData)"-->
      <!--v-show="!checkBoxIcon">批量管理-->
      <!--</el-button>-->
      <!--<el-button type="info" size="small" plain @click="toggleSelection()" v-show="checkBoxIcon">取消批量管理</el-button>-->
      <!--<el-button type="danger" size="small" icon="el-icon-delete" class="button-position" v-if="checkBoxIcon"-->
      <!--@click="batchDel">删除选中-->
      <!--</el-button>-->
    </div>

    <div class="circleManage-table">
      <el-table ref="multipleTable" :data="circleData" border stripe fit v-loading="listLoading"
                :row-style="{height:'50px'}" :header-row-style="{height:'40px'}"
                @selection-change="handleSelectionChange" :row-key="getRowKeys">
        <el-table-column type="selection" :reserve-selection="true" width="40" v-if="checkBoxIcon"></el-table-column>
        <el-table-column prop="id" label="id" width="80">
          <template slot-scope="scope">
            {{scope.row.id}}
          </template>
        </el-table-column>
        <el-table-column label="用户昵称" width="150">
          <template slot-scope="scope">
            {{scope.row.userName}}
          </template>
        </el-table-column>
        <el-table-column label="圈子图片" width="450">
          <template slot-scope="scope">
            <viewer :images="scope.row.moodUrl">
              <img :src="item?item:require('../../../assets/common_images/placeholder-pic.png')" class="circle_pic"
                   v-for="(item,index) in scope.row.moodUrl"
                   :key="index"/>
            </viewer>
          </template>
        </el-table-column>
        <el-table-column label="是否拉黑" width="100">
          <template slot-scope="scope">
            {{scope.row.defriend | changFriend}}
          </template>
        </el-table-column>
        <el-table-column label="是否删除" width="100">
          <template slot-scope="scope">
            {{scope.row.delStatus | changeIsDel}}
          </template>
        </el-table-column>
        <el-table-column label="生成时间" width="160">
          <template slot-scope="scope">
            <span v-if="scope.row.time">{{scope.row.time | formatTime}}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" min-width="200" fixed="right">
          <template slot-scope="{row}">
            <el-dropdown size="mini" :split-button="true" type="primary" @click="handleLook(row)"
                         v-if="checkPermissionPoints(3) == 1">查看
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleDelete(row)" v-if="checkPermissionPoints(2) == 1">删除
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleBlock(row,1)"
                                  v-if="checkPermissionPoints(2) == 1 && row.defriend == 0">拉黑
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleBlock(row,0)"
                                  v-if="checkPermissionPoints(2) == 1 && row.defriend == 1">取消拉黑
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :type="1" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                  @pagination="getCircleList"/>
    </div>

    <!--查看-->
    <el-dialog title="详细信息" :visible.sync="dialogLookVisible" width="960px" class="circleManage-dialog look-dialog">
      <el-form ref="form" label-width="140px" size="mini">
        <el-form-item label="用户昵称：">
          <span>{{circle.userName}}</span>
        </el-form-item>
        <el-form-item label="圈子内容：">
          <span>{{circle.moodDetails}}</span>
        </el-form-item>
        <el-form-item label="圈子图片：">
          <img v-lazy="item?item:require('../../../assets/common_images/placeholder-pic.png')" class="circle_pic2"
               v-for="(item,k) in circle.moodUrl"
               :key="k"/>
        </el-form-item>
        <el-form-item label="是否拉黑：">
          <span>{{circle.defriend | changFriend}}</span>
        </el-form-item>
        <el-form-item label="是否删除：">
          <span>{{circle.delStatus | changeIsDel}}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" style="width: 96px" @click="dialogLookVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
  import Pagination from '@/components/Pagination';
  import {mapGetters} from 'vuex';
  import {permissionPoints} from '@/utils/permission';
  import {formatDate} from "@/utils/index";
  import {deFriend, deleteCircle, getCircleDetail, getInitCircleList} from "../../../api/social/social";

  export default {
    name: "circleManage",
    computed: {
      ...mapGetters([
        'points'
      ])
    },
    components: {
      Pagination
    },
    data() {
      return {
        multipleSelection: [],
        checkBoxIcon: false,
        total: 0,
        listQuery: {
          page: 1,
          limit: 20,
          userName: ''
        },
        keyWords: '',
        select: '1',
        listLoading: false,
        dialogLookVisible: false,
        circleData: [],
        circle: {}
      }
    },
    created() {
      this.getCircleList();
    },
    methods: {
      searchCircle() {//搜索
        if (this.keyWords == '' || this.keyWords == undefined) {
          return this.$message.info({message: '请输入搜索内容', duration: 1000});
        }
        this.listQuery.userName = '';
        if (this.select == 1) {
          this.listQuery.userName = this.keyWords;
        }
        this.listQuery.page = 1;
        this.getCircleList();
      },
      resetSearch() {
        this.select = '1';
        this.keyWords = '';
        this.listQuery.userName = '';
        this.listQuery.page = 1;
        this.getCircleList();
      },
      checkPermissionPoints(type) {
        let pointObj = this.points.circleManage;
        return permissionPoints(type, pointObj);
      },
      getCircleList() {
        this.listLoading = true;
        getInitCircleList(this.listQuery).then((res) => {
          this.listLoading = false;
          let retObj = res;
          if (retObj.code == 0 && retObj.data != null) {
            const {items, totalRecord} = retObj.data;
            this.total = totalRecord;

            items.forEach((item) => {
              item.moodUrl = item.moodUrl.split(';');
            });
            this.circleData = items;
          }
        }).catch(() => {
          this.listLoading = false;
        })
      },
      handleLook(row) {//详细信息
        this.dialogLookVisible = true;
        getCircleDetail(row.id).then((res) => {
          let retObj = res;
          if (retObj.code == 0 && retObj.data != null) {
            this.circle = retObj.data;
            this.circle.moodUrl = retObj.data.moodUrl.split(';');
          }
        })
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      getRowKeys(row) {
        return row.id;
      },
      toggleSelection(rows) {
        if (rows) {
          this.checkBoxIcon = !this.checkBoxIcon;
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.checkBoxIcon = false;
          this.$refs.multipleTable.clearSelection();
        }
      },
      batchDel() {//批量
        let row = this.commonSelection();
        if (row && Object.keys(row).length != 0) {
          this.handleDelete(row);
        }
      },
      commonSelection() {
        let row = {};
        let value = this.multipleSelection;
        if (value && value.length > 0) {
          let arr = [];
          value.forEach((item) => {
            arr.push(item.id);
          });
          row.id = arr.join(",");
        }
        return row;
      },
      handleDelete(row) {//删除
        this.$confirm('此操作将删除该圈子, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteCircle(row.id).then((res) => {
            let retObj = res;
            if (retObj.code == 0 && retObj.data > 0) {
              this.$message.success({message: '删除成功', duration: 2000});
              this.toggleSelection();
              this.getCircleList();
            } else {
              this.$message.error({message: '删除失败，请重试', duration: 2000});
            }
          })
        }).catch((err) => {
        });
      },
      handleBlock(row, type) {
        this.$confirm(`此操作将${type == 1 ? '拉黑' : '取消拉黑'}用户, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deFriend(row.id, type).then((res) => {
            let retObj = res;
            if (retObj.code == 0 && retObj.data > 0) {
              this.$message.success({message: type == 1 ? '拉黑成功' : '取消拉黑成功', duration: 2000});
              this.getCircleList();
            } else {
              this.$message.error({message: type == 1 ? '拉黑失败，请重试' : '取消拉黑失败，请重试', duration: 2000});
            }
          })
        }).catch((err) => {
        });
      }
    },
    filters: {
      formatTime(time) {
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss');
      },
      changFriend(status) {
        if (status == 0) {
          return '正常';
        } else if (status == 1) {
          return '拉黑';
        }
      },
      changeIsDel(type) {
        if (type == 0) {
          return '否';
        } else if (type == 1) {
          return '是';
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .circleManage-container {
    .circleManage-search {
      display: flex;
      align-items: center;
      width: 100%;
      height: 80px;
      .page-title {
        padding-left: 18px;
        min-width: 160px;
        text-align: left;
        font-size: 18px;
        font-weight: bold;
        color: rgba(61, 68, 77, 1);
      }
      .search-keyword {
        margin-right: 24px;
        width: 470px;
        > > > .el-select .el-input {
          width: 110px;
        }
        > > > .el-input-group__prepend {
          background-color: #fff;
        }
      }
      .search-keyword-reset {
        margin-right: 36px;
      }
      .excel-inout-box {
        margin-left: auto;
        margin-right: 18px;
      }
    }

    .circleManage-table {
      width: 98%;
      margin-left: 18px;
      > > > .el-table td, .el-table th {
        padding: 6px 0px;
      }
      > > > .el-table__header th {
        padding: 0;
        height: 30px;
        line-height: 30px;
      }
      > > > .el-table__fixed-right {
        height: 100% !important;
      }
      > > > .el-table__column-filter-trigger {
        line-height: 28px;
        i {
          font-size: 18px;
        }
      }
      > > > .el-table__row .cell {
        display: flex;
        align-items: center;
      }
      > > > .el-table__header .has-gutter .cell {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }

    .circleManage-dialog > > > .el-dialog .el-dialog__header {
      display: flex;
      align-items: center;
      height: 48px;
      background: #F7F8FA;
      padding: 0px 0px 0px 12px;
      .el-dialog__headerbtn {
        top: 15px;
      }
    }
    .look-dialog > > > .el-form-item--mini.el-form-item, .el-form-item--small.el-form-item {
      margin-bottom: 0px;
    }
    .circleManage-dialog {
      font-size: 14px;
      color: #606266;
    }
    .circle_pic {
      margin-left: 5px;
      object-fit: cover;
      width: 40px;
      height: 40px;
      border-radius: 2px;
    }
    .circle_pic2 {
      margin-left: 5px;
      object-fit: cover;
      width: 72px;
      height: 72px;
      border-radius: 2px;
    }
    > > > .el-button {
      line-height: 1.2;
    }
  }
</style>
